﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF_基础1.Datas
{
    public class PigeonDbContext:DbContext
    {
        /// <summary>
        /// 日志，可把Sql日志打印到控制台
        /// </summary>
        public readonly ILoggerFactory loggerFactory =
            LoggerFactory.Create(builder => { builder.AddConsole(); });

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //设置连接属性
            string connectionStr =
                "Data Source=127.0.0.1;Initial Catalog=EFDb;User Id=sa;Password=Ronaldo6!;Encrypt=True;TrustServerCertificate=True;";
            optionsBuilder.UseSqlServer(connectionStr);
            //添加日志工厂
            optionsBuilder.UseLoggerFactory(loggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //加载当前程序集下，所有的实体配置文件
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }
    }
}
